﻿/* **********************************************************************************
 * Copyright (c) 2011 John Hughes
 *
 * Elve Touch is licenced under the Microsoft Reciprocal License (Ms-RL).
 *
 * Project Website: http://elvetouch.codeplex.com/
 * **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ElveTouch
{
    public partial class SignIn : Form
    {
        public SignIn(string message, string username, string password, string hostname, int port, RenderingMode renderingMode)
        {
            InitializeComponent();

            txtPassword.PasswordChar = '\u25CF'; // black circle
            ddlQuality.SelectedIndex = 1;

            lblMessage.Text = message;

            txtUserName.Text = username;
            txtPassword.Text = password;
            txtServer.Text = hostname;
            //nudPort.Value = port;
            ddlQuality.SelectedIndex = (int)renderingMode;

            string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Codecore Technologies\Elve\TouchScreenViewerLite.config");
            chkRememberSettings.Checked = System.IO.File.Exists(filePath);
        }
        
        private void pbClose_Click(object sender, EventArgs e)
        {
            //DialogResult = DialogResult.Cancel;
            Environment.Exit(0);
        }

        public string UserName
        {
            get { return txtUserName.Text; }
        }

        public string Password
        {
            get { return txtPassword.Text; }
        }

        public string Hostname
        {
            get
            {
                // Syntax: hostname -or- hostname:port
                int pos = txtServer.Text.LastIndexOf(':');
                if (pos == -1)
                    return txtServer.Text;
                else
                    return txtServer.Text.Substring(0, pos);
            }
        }

        public int Port
        {
            get
            {
                // Syntax: hostname -or- hostname:port
                int pos = txtServer.Text.LastIndexOf(':');
                if (pos == -1)
                    return 33907; // default port
                else
                    return int.Parse(txtServer.Text.Substring(pos + 1));
            }
        }

        public int Quality
        {
            get { return ddlQuality.SelectedIndex; }
        }

        public bool RememberSettings
        {
            get { return chkRememberSettings.Checked; }
        }

        private void lblDone_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                txtPassword.Focus();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                txtServer.Focus();
        }

        private void txtServer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                ddlQuality.Focus();
        }

        private void ddlQuality_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                DialogResult = DialogResult.OK;
        }

        private void SignIn_FormClosed(object sender, FormClosedEventArgs e)
        {
            //_touchServiceDiscovery.Dispose();
        }
    }
}
